package com.heu.blood.charge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.charge.entity.ChargeApplicationEntity;
import com.heu.blood.charge.entity.ChargeItemEntity;
import com.heu.blood.recycle.entity.BloodTrackEntity;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.charge.dao.ChargeItemApplicationDao;
import com.heu.blood.charge.entity.ChargeItemApplicationEntity;
import com.heu.blood.charge.service.ChargeItemApplicationService;


@Service
public class ChargeItemApplicationServiceImpl extends ServiceImpl<ChargeItemApplicationDao, ChargeItemApplicationEntity> implements ChargeItemApplicationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        ChargeItemApplicationEntity entity = new ChargeItemApplicationEntity();
        if(Strings.isNotBlank((String) params.get("applicationId")))
            entity.setApplicationId((String) params.get("applicationId"));
        if(Strings.isNotBlank((String) params.get("patientId")))
            entity.setPatientId((String) params.get("patientId"));
        if(Strings.isNotBlank((String) params.get("chargeId")))
            entity.setChargeId((String) params.get("chargeId"));
        if(Strings.isNotBlank((String) params.get("chargeType")))
            entity.setChargeId((String) params.get("chargeType"));
        if(Strings.isNotBlank((String) params.get("chargeProject")))
            entity.setChargeId((String) params.get("chargeProject"));
        if(Strings.isNotBlank((String) params.get("operator")))
            entity.setOperator((String) params.get("operator"));
        if(Strings.isNotBlank((String) params.get("isReturn")))
            entity.setIsReturn(Integer.valueOf((String) params.get("isReturn")));

        //添加时间范围条件
        String startTime = null;
        String endTime = null;
        if(Strings.isNotBlank((String)params.get("startTime")))
            startTime = (String)params.get("startTime");
        if(Strings.isNotBlank((String)params.get("endTime")))
            endTime = (String)params.get("endTime");



        LambdaQueryWrapper<ChargeItemApplicationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(entity.getApplicationId()), ChargeItemApplicationEntity::getApplicationId,entity.getApplicationId())
                .eq(Strings.isNotBlank(entity.getChargeId()),ChargeItemApplicationEntity::getChargeId,entity.getChargeId())
                .eq(Strings.isNotBlank(entity.getPatientId()),ChargeItemApplicationEntity::getPatientId,entity.getPatientId())
                .eq(Strings.isNotBlank(entity.getChargeType()),ChargeItemApplicationEntity::getChargeType,entity.getChargeType())
                .eq(Strings.isNotBlank(entity.getChargeProject()),ChargeItemApplicationEntity::getChargeProject,entity.getChargeProject())
                .eq(Strings.isNotBlank(entity.getOperator()),ChargeItemApplicationEntity::getOperator,entity.getOperator())
                .eq(entity.getIsReturn() != null,ChargeItemApplicationEntity::getIsReturn,entity.getIsReturn())
                .ge(Strings.isNotBlank(startTime),ChargeItemApplicationEntity::getChargeTime,startTime)
                .le(Strings.isNotBlank(endTime),ChargeItemApplicationEntity::getChargeTime,endTime)
                .eq(ChargeItemApplicationEntity::getIfShow,1);



        IPage<ChargeItemApplicationEntity> page = this.page(
                new Query<ChargeItemApplicationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}